/**
 * JAVACC DEMO 1.0
 * @copy right dwusoft company All rights reserved. 
 * @Package com.apache.uct.service.plugins  
 */
package com.apache.uct.service.plugins;

import com.apache.api.manager.PluginConnector;
import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultEntity;
import com.apache.cache.util.Validator;
import com.apache.tools.ClassToolsUtil;
import com.apache.uct.common.entity.Dept;
import com.apache.uct.manager.DeptManager;

/**
 * description:  部门信息保存
 * @Copyright(c)2016:软件技术有限公司
 */
public class DeptSavePlugin implements PluginConnector {

	private DeptManager deptManager;

	public Object execute(ParamsVo vo) throws Exception {
		ResultEntity result = new ResultEntity();
		Dept dept = new Dept();
		dept = (Dept) ClassToolsUtil.getInstance().convert2Bean(vo.getParams(), dept);
		try {
			result.setResult("true");
			String doCode = String.valueOf(vo.getParams("doCode"));
			String deptId = String.valueOf(vo.getParams("deptId"));
			if ("deptSave".equals(doCode) && Validator.isNull(deptId)) {
				addDept(result, vo);
			} else if ("deptSave".equals(doCode) && Validator.isNotNull(deptId)) {
				editDept(result, vo);
			} else {
				result.setMessage("没有可用方法被执行！");
			}
		} catch (Exception e) {
			result.setResult("false");
			result.setMessage("执行失败！");
		}
		return result;
	}

	private String getParamStr(ParamsVo vo, String attrName) {
		return Validator.getDefaultStr(String.valueOf(vo.getParams(attrName)), "");
	}

	private void addDept(ResultEntity result, ParamsVo vo) {
		Dept dept = new Dept();
		dept = (Dept) ClassToolsUtil.getInstance().convert2Bean(vo.getParams(), dept);
		ParamsVo<Dept> deptVo = new ParamsVo<Dept>();
		deptVo.setObj(dept);
		String deptId = deptManager.saveInfo(deptVo);
		result.setEntity(deptId);
		result.setMessage("操作成功！");
	}

	private void editDept(ResultEntity result, ParamsVo vo) {
		Dept dept = new Dept();
		dept = (Dept) ClassToolsUtil.getInstance().convert2Bean(vo.getParams(), dept);

	}

}
